#ifndef BOOK_EXT_H
#define BOOK_EXT_H

#include "extension.h"
#include <stdio.h>
#include <string.h>

// 基础结构体定义
typedef struct {
    char title[50];  // 书名
    char author[30]; // 作者
    float price;     // 价格
    int pages;       // 页数
} Book;

/**
 * 初始化Book实例
 */
static inline void Book_init(Book* self, const char* title, const char* author,
                           float price, int pages) {
    strncpy(self->title, title, sizeof(self->title) - 1);
    self->title[sizeof(self->title) - 1] = '\0';  // 确保字符串终止

    strncpy(self->author, author, sizeof(self->author) - 1);
    self->author[sizeof(self->author) - 1] = '\0';  // 确保字符串终止

    self->price = price;
    self->pages = pages;
}

// --------------------------
// 扩展函数声明
// --------------------------

/**
 * 打印书籍信息
 */
DEFINE_EXTENSION(Book, print_info, void);

/**
 * 判断是否为厚书
 * @param min_pages 最小页数标准
 * @return 达到标准返回true，否则返回false
 */
DEFINE_EXTENSION(Book, is_thick, bool, int min_pages);

/**
 * 计算折扣后价格
 * @param discount 折扣比例（0-1之间）
 * @param is_member 是否为会员（会员额外8折）
 * @return 折扣后价格
 */
DEFINE_EXTENSION(Book, get_discounted_price, float, float discount, bool is_member);

// --------------------------
// 扩展函数实现
// --------------------------

DEFINE_EXTENSION(Book, print_info, void) {
    printf("[Book] 《%s》（作者：%s），价格：%.2f元，页数：%d\n",
           self->title, self->author, self->price, self->pages);
}

DEFINE_EXTENSION(Book, is_thick, bool, int min_pages) {
    return self->pages >= min_pages;
}

DEFINE_EXTENSION(Book, get_discounted_price, float, float discount, bool is_member) {
    float final_discount = discount;
    if (is_member) {
        final_discount *= 0.8;  // 会员额外8折
    }
    return self->price * (1 - final_discount);
}

#endif // BOOK_EXT_H
